package com.tbit.uqbike.service.business.impl;

import com.tbit.common.constant.OperationPlatform;
import com.tbit.common.entity.UserOpLog;
import com.tbit.uqbike.object.business.UserInfo;
import com.tbit.uqbike.object.pojo.CouponMeal;
import com.tbit.uqbike.service.business.CouponMealService;
import com.tbit.uqbike.webmanager.business.user_op_log.service.UserOpLogService;
import com.tbit.uqbike.webmanager.controller.BaseController;
import com.tbit.uqbike.webmanager.dao.core.CouponMealDao;
import com.tbit.uqbike.webmanager.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 优惠券套餐Service业务层处理
 *
 * @author jing
 * @date 2021-05-25
 */
@Service
public class CouponMealServiceImpl implements CouponMealService {
    @Autowired
    private CouponMealDao couponMealDao;
    @Autowired
    private UserOpLogService userOpLogService;

    /**
     * 查询优惠券套餐
     *
     * @param mealId 优惠券套餐ID
     * @return 优惠券套餐
     */
    @Override
    public CouponMeal selectCouponMealById(Integer mealId) {
        return couponMealDao.selectCouponMealById(mealId);
    }

    /**
     * 查询优惠券套餐列表
     *
     * @param couponMeal 优惠券套餐
     * @return 优惠券套餐
     */
    @Override
    public List<CouponMeal> selectCouponMealList(CouponMeal couponMeal) {
        return couponMealDao.selectCouponMealList(couponMeal);
    }

    @Override
    public Integer selectCouponMealCount(CouponMeal couponMeal) {
        return couponMealDao.selectCouponMealCount(couponMeal);
    }

    /**
     * 新增优惠券套餐
     *
     * @param couponMeal 优惠券套餐
     * @return 结果
     */
    @Override
    public int insertCouponMeal(CouponMeal couponMeal) {
        couponMeal.setAddTime(DateUtils.getNowDate());
        int result = couponMealDao.insertCouponMeal(couponMeal);

        // 插入用户操作日志
        UserInfo loginInfo = BaseController.fetchUserInfo();
        userOpLogService.insertSingle(UserOpLog.builder()
                .accountId(couponMeal.getAccountId())
                .opUser(loginInfo.getName())
                .opUserPhone(loginInfo.getPhone())
                .operation("优惠套餐")
                .opType("创建")
                .opPlatform(OperationPlatform.MANAGER.code)
                .opTime(LocalDateTime.now())
                .detail("优惠套餐名称: " + couponMeal.getName())
                .result(true)
                .build());

        return result;
    }

    /**
     * 修改优惠券套餐
     *
     * @param couponMeal 优惠券套餐
     * @return 结果
     */
    @Override
    public int updateCouponMeal(CouponMeal couponMeal) {
        return couponMealDao.updateCouponMeal(couponMeal);
    }

    /**
     * 批量删除优惠券套餐
     *
     * @param mealIds 需要删除的优惠券套餐ID
     * @return 结果
     */
    @Override
    public int deleteCouponMealByIds(Integer[] mealIds) {
        return couponMealDao.deleteCouponMealByIds(mealIds);
    }

    /**
     * 删除优惠券套餐信息
     *
     * @param mealId 优惠券套餐ID
     * @return 结果
     */
    @Override
    public int deleteCouponMealById(Integer mealId) {
        return couponMealDao.deleteCouponMealById(mealId);
    }
}
